/**
 * 通用uni-app网络请求
 * 基于 Promise 对象实现更简单的 request 使用方式，支持请求和响应拦截
 */
import store from '@/store/index'

export default {
	config: {
		baseUrl: '',
		header: {
			'Content-Type': 'application/x-www-form-urlencoded'
		},
		data: {},
		method: "GET",
		dataType: "json",
		/* 如设为json，会对返回的数据做一次 JSON.parse */
		responseType: "text",
		success() {},
		fail() {},
		complete() {}
	},
	interceptor: {
		request: (config) => {
			config.header.token = store.state.token
		},
		response: (response) => {
			let statusCode = response.statusCode

			if (statusCode === 200) {
				return response.data
			} else if (statusCode === 401) {
				store.commit('logout');
				uni.switchTab({url: '/pages/my/index'})
				uni.showToast({
					title: response.data.msg,
					icon: 'none'
				})
				return response.data
			} else {
				if (response.hasOwnProperty('data') && response.data.hasOwnProperty('msg')) {
					return response.data
				} else {
					return response.data = {
						code: 1,
						data: null,
						msg: '网络请求出现故障,请稍后重试'
					}
				}
			}
		}
	},
	async request(options) {
		// uni.showLoading({  
		// 	title: '加载中'  
		// });
		if (!options) {
			options = {}
		}
		if(process.env.NODE_ENV === 'development'){
			// this.config.baseUrl = 'https://yhb.xingchenpay.cn/api'
			this.config.baseUrl = 'https://duofu.yycjkb.cn/api'
			// this.config.baseUrl = 'https://yl.weihankeji.com/api'
			// this.config.baseUrl = 'http://zsff.xs334.cn/api'
			// this.config.baseUrl = 'http://f.mjmall.xyz/api'
			// #ifdef H5
			this.config.baseUrl = 'https://duofu.yycjkb.cn/api'
			// this.config.baseUrl = 'https://yl.weihankeji.com/api'
			// #endif
		} else {
			this.config.baseUrl = 'https://duofu.yycjkb.cn/api'
			// this.config.baseUrl = 'https://yl.weihankeji.com/api'
			// this.config.baseUrl = 'https://yhb.xingchenpay.cn/api'
			// this.config.baseUrl = 'http://zsff.xs334.cn/api'
			// this.config.baseUrl = 'http://f.mjmall.xyz/api'
			// #ifdef H5
			this.config.baseUrl = 'https://duofu.yycjkb.cn/api'
			// this.config.baseUrl = 'https://yl.weihankeji.com/api'
			// #endif
		}

		options.baseUrl = options.baseUrl || this.config.baseUrl
		options.dataType = options.dataType || this.config.dataType
		options.url = options.baseUrl + options.url
		options.data = options.data || {}
		options.method = options.method || this.config.method
		//TODO 加密数据

		//TODO 数据签名
		/* 
		_token = {'token': getStorage(STOREKEY_LOGIN).token || 'undefined'},
		_sign = {'sign': sign(JSON.stringify(options.data))}
		options.header = Object.assign({}, options.header, _token,_sign) 
		*/

		return new Promise((resolve, reject) => {
			let _config = null

			options.complete = (response) => {
				response.config = _config

				if (this.interceptor.response) {
					let newResponse = this.interceptor.response(response)
					if (newResponse) {
						response = newResponse
					}
				}
        uni.hideLoading();
				// 统一的响应日志记录
				_reslog(response)

				resolve(response);
			}

			_config = Object.assign({}, this.config, options)
			_config.requestId = new Date().getTime()

			if (this.interceptor.request) {
				this.interceptor.request(_config)
			}

			// 统一的请求日志记录
			_reqlog(_config)

			uni.request(_config);
		});
	},
	get(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	post(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
	put(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'PUT'
		return this.request(options)
	},
	delete(url, data, options) {
		if (!options) {
			options = {}
		}
		options.url = url
		options.data = data
		options.method = 'DELETE'
		return this.request(options)
	}
}


/**
 * 请求接口日志记录
 */
function _reqlog(req) {
	// if (process.env.NODE_ENV === 'development') {
	// 	console.log("【" + req.requestId + "】 地址：" + req.url)
	// 	if (req.data) {
	// 		console.log("【" + req.requestId + "】 请求参数：" + JSON.stringify(req.data))
	// 	}
	// }
	//TODO 调接口异步写入日志数据库
}

/**
 * 响应接口日志记录
 */
function _reslog(res) {
	// let _statusCode = res.statusCode;
	// if (process.env.NODE_ENV === 'development') {
	// 	console.log("【" + res.config.requestId + "】 地址：" + res.config.url)
	// 	if (res.config.data) {
	// 		console.log("【" + res.config.requestId + "】 请求参数：" + JSON.stringify(res.config.data))
	// 	}
	// 	console.log("【" + res.config.requestId + "】 响应结果：" + JSON.stringify(res))
	// }
	// //TODO 除了接口服务错误外，其他日志调接口异步写入日志数据库
	// switch (_statusCode) {
	// 	case 200:
	// 		break;
	// 	case 401:
	// 		break;
	// 	case 404:
	// 		break;
	// 	default:
	// 		break;
	// }
}
